<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');

	class m_Paymentaccounts extends CI_Model
	{
		var $accountId = 0;
		var $accountName = '';
		var $userId = 0;
		var $accountNumber = '';
		var $accountType = '';

    	function account_exist()
    	{
			$query1 = $this->db->get_where('tblPaymentAccounts', array('accountNumber' => $this->accountNumber, 'userId' => $this->userId));
			$query2 = $this->db->get_where('tblPaymentAccounts', array('accountId' => $this->accountId, 'userId' => $this->userId));
			return (sizeof($query1->row()) > 0) || (sizeof($query2->row()) > 0);
    	}

		function __construct()
    	{
    	    // Call the Model constructor
    	    parent::__construct();
    	}

    	function set_account_id($aid)
    	{
    		$this->accountId = $aid;
    	}

    	function set_account_name($aname)
    	{
    		$this->accountName = $aname;
    	}

    	function set_user_id($uid)
    	{
    		$this->userId = $uid;
    	}

    	function set_account_number($anum)
    	{
    		$this->accountNumber = $anum;
    	}

    	function set_account_type($atype)
    	{
    		$this->accountType = $atype;
    	}

    	function get_payment_account($accountId)
    	{
			$this->load->database();
			$query = $this->db->get_where('tblPaymentAccounts', array('accountId' => $accountId));
			if ((sizeof($query->row()) > 0))
			{
				$this->set_account_id($query->row()->accountId);
				$this->set_account_name($query->row()->accountName);
				$this->set_user_id($query->row()->userId);
				$this->set_account_number($query->row()->accountNumber);
				$this->set_account_type($query->row()->accountType);
			}
			return $this;
    	}

    	function get_payment_accounts($userId)
    	{
			$this->load->database();
			$this->load->library('Datatables');
			$this->datatables->select('accountName, accountType, accountNumber, accountId')->from('tblPaymentAccounts')->where('userId', $userId);
			$query = $this->datatables->generate();
			return $query;
    	}

		function delete_payment_account()
		{
			$this->db->delete('tblPaymentAccounts', array('accountId' => $this->accountId)); 
		}

		function edit_payment_account()
		{
			if($this->account_exist())
			{
				$this->update_payment_account($this);
			}
			else
			{
				$this->insert_payment_account($this);
			}
		}

    	function insert_payment_account()
    	{
			$result = $this->db->insert('tblPaymentAccounts', $this);
			return $result;
		}

		function update_payment_account()
		{
			$this->db->where('accountId', $this->accountId);
			$result = $this->db->update('tblPaymentAccounts', $this);
			return $result;
		}
	}
?>
